﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Daphne.GameShared;

namespace GameEngine.PDNParser
{
    public class PDNReader
    {
        private Localizer _loc = new Localizer();

        public PDNReader()
        {
        }

        public string ErrorMessage
        { get; set; }

        public bool ParseFile(Stream s, out PDNRawGameList gameList)
        {
            gameList = null;

            try
            {
                PDNScanner scanner = new PDNScanner(_loc);
                var tokens = scanner.ScanFile(s);

                PDNLexer lex = new PDNLexer(_loc);
                var lexList = lex.Parse(tokens);

                PDNSyntaxParser syntaxParser = new PDNSyntaxParser(_loc);
                if (syntaxParser.Parse(lexList, out gameList) == false)
                {
                    ErrorMessage = syntaxParser.ErrorMessage;
                    return false;
                }
            }
            catch(Exception ex)
            {
                this.ErrorMessage = ex.Message;
                return false;
            }

            return true;
        }

        public bool InterpretGame(PDNRawGame game, GameManager gm)
        {
            try
            {
                PDNInterpreter interpreter = new PDNInterpreter();
                interpreter.InterpretGame(game, gm);
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
                return false;
            }
            return true;
        }

        private class Localizer: ILocalizer
        {
            public string  GetStringByKey(string sKey)
            {
 	            return string.Empty;
            }

            public string  FormatMessage(string sKey, params object[] parameters)
            {
 	            return string.Format(sKey, parameters);
            }
        }
   
    }
}
